package cn.edu.nankai.bookstore.controller.customer;

import cn.edu.nankai.bookstore.domain.po.Customer;
import cn.edu.nankai.bookstore.service.CustomerService;
import cn.edu.nankai.bookstore.service.impl.CustomerServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LogInServlet", value = "/LogInServlet")
public class LogInServlet extends HttpServlet {
    private CustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("user") != null){
            response.getWriter().write("true");
            return;
        }
        String tel = (String) request.getParameter("tel");
        String pwd = (String) request.getParameter("pwd");
        if(tel.length() == 0 || pwd.length() == 0 ) {
            response.getWriter().write("false");
            return;
        }
        Customer customer = customerService.isValidate(tel, pwd);
        if(customer == null){
            response.getWriter().write("false");
            return;
        }
        session.setAttribute("user",customer);
        response.getWriter().write("true");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
